package com.skivingcloud.admin.sys.controller;


import java.util.ArrayList;
import java.util.List;

import com.skivingcloud.admin.annotation.UserOperateLog;
import com.skivingcloud.admin.sys.entity.SysOrg;
import com.skivingcloud.admin.sys.entity.User;
import com.skivingcloud.admin.sys.service.ISysOrgService;
import com.skivingcloud.admin.utils.UserUtil;
import com.skivingcloud.common.contants.Constant;
import com.skivingcloud.common.utils.ElTreeUtils;
import com.skivingcloud.common.utils.R;
import com.skivingcloud.common.utils.StringUtils;
import com.skivingcloud.common.utils.TreeUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 组织机构表 前端控制器
 * </p>
 *
 * @author skivingcloud
 * @since 2022-01-29
 */
@RestController
@RequestMapping("/sys/org")
public class SysOrgController {

	private static Logger log = LoggerFactory.getLogger(SysOrgController.class);

	@Autowired
	private ISysOrgService sysOrgService;

	/**
	 * 获取树形机构
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/treeList")
	@ResponseBody
	public List<TreeUtils> treeList() throws Exception {
		User user = UserUtil.getUser();
		if (!Constant.SUPER_ADMIN_TENANTID.equals(user.getTenantId())) {
			return sysOrgService.getAllOrgTreeUtils(user.getTenantId());
		} else {
			return sysOrgService.getAllOrgTreeUtils();
		}
	}

	/**
	 * 获取树形机构
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/treeList/{tenantId}")
	@ResponseBody
	public List<TreeUtils> treeList(@PathVariable("tenantId") String tenantId) throws Exception {
		if (StringUtils.isBlank(tenantId)) {
			return new ArrayList<TreeUtils>();
		}
		return sysOrgService.getAllOrgTreeUtils(tenantId);
	}

	/**
	 * 获取树形机构
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/treeListEltree/{tenantId}")
	@ResponseBody
	public List<ElTreeUtils> treeListEltree(@PathVariable("tenantId") String tenantId) throws Exception {
		if (StringUtils.isBlank(tenantId)) {
			return new ArrayList<ElTreeUtils>();
		}
		return sysOrgService.getAllOrgTreeUtilsEl(tenantId);
	}

	/**
	 * 保存机构数据
	 * 
	 * @param org
	 * @return
	 */
	@UserOperateLog(module = "系统功能", methods = "save", description = "新增或修改机构信息")
	@PostMapping("/save")
	public R save(@RequestBody SysOrg org) {
		try {
			if (org.getId() == null || "".equals(org.getId())) {
				// 新增
				sysOrgService.addOrg(org, UserUtil.getUser());
			} else {
				// 修改
				sysOrgService.updateOrg(org, UserUtil.getUser());
			}
			return R.ok();
		} catch (Exception e) {
			log.error("保存机构信息失败", e);
			return R.error("保存机构信息失败");
		}
	}

	/**
	 * 根据id删除机构
	 * 
	 * @return
	 */
	@UserOperateLog(module = "系统功能", methods = "deleteById", description = "删除机构信息")
	@PostMapping("/deleteById")
	public R deleteById(@RequestBody  SysOrg org) {
		try {
			sysOrgService.deleteById(org.getId(), UserUtil.getUser());
			return R.ok();
		} catch (Exception e) {
			log.error("删除机构信息失败", e);
			return R.error("删除机构信息失败");
		}
	}
}
